home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / System / DX Clock 1.31 / DX Clockƒ / DX Clock.c org < prev    next >
Encoding:
Text File  |  1991-08-29  |  10.1 KB  |  416 lines  |  [TEXT/KAHL]

  1. /*
  2.     DX Clock.c
  3.     13 août 1991
  4.     Stephan Burlot 1991
  5.  
  6.     patches SystemTask to draw a clock in the menu bar.
  7.  
  8.     we use DrawString instead of multiple calls to DrawChars because some fonts are
  9.     kerned and if we use DrawChar in srcCopy, it erases the right side of the previous
  10.     char.
  11.     
  12.     this patch should be installed as a routine in JGNEFilter, so it would be
  13.     easier to catch a click in the menu bar.
  14.  
  15. */
  16.  
  17. #include    <color.h>                /* some info about color quickdraw */
  18.  
  19. #define    RowBits            0x0C20        ; pixels horizontally
  20. #define    WWExist            0x08F2        ; Window Manager Initialized ?
  21. #define    WMgrCPort        0x0D2C        ; [GLOBAL VAR]  window manager color port
  22. #define    WMgrPort        0x09DE        ; Window Manager Port
  23. #define SystemTask        0xA9B4        ; System Task trap number
  24. #define    GetNextEvent    0xA970        ; GetNextEvent trap number
  25. #define    Unimplemented    0x9F        ; Unimplemented trap number
  26. #define MBState            0x0172        ; Mouse Button state
  27. #define    Mouse             0x0830        ; mouse loc
  28. #define    FractEnable        0x0BF4         ; fractionnal width allowed ? (byte)
  29. #define    MenuList        0x0A1C        ; menu list
  30. #define    MBTicks         0x016E        ; tick count @ last mouse button [long]
  31. #define    MenuCInfo        0x0D50        ; handle to Menu Color Info
  32. #define    RGBBlack        0x0C10        ; RGB color black
  33. #define    RGBWhite        0x0C16        ; RGB Color white
  34. #define    ChunkyDepth        0x0D60        ; [GLOBAL VAR]  depth of the pixels
  35.  
  36. #define    mctRGB1            0x4
  37. #define    mctRGB4            0x16
  38.  
  39. #define    JMP                0x4EF9
  40.  
  41. #define    tnam            0
  42. #define    pointer            4
  43. #define    font            8
  44. #define    size            10
  45. #define    offset            12
  46. #define    stringwid        14
  47. #define    vpos            16
  48. #define    red                18
  49. #define    green            20
  50. #define    blue            22
  51. #define    therect            24
  52. #define    toprect            24
  53. #define    leftrect        26
  54. #define    bottomrect        28
  55. #define    rightrect        30
  56. #define    useclockcolor    32
  57.  
  58. pascal void ShowINIT() ;
  59.  
  60. void Main(void) ;
  61.  
  62. void main()
  63.  
  64. {
  65.     asm    {
  66.  
  67.     move.l    D4,-(SP)            ; save some registers
  68.  
  69.     tst.b    MBState                ; mouse button down ?
  70.     bpl        @noPatch            ; yes, dont install patch
  71.  
  72.     move.w    #SystemTask,D0        ; get the address of the trap
  73.     _GetTrapAddress                ; result is in A0
  74.  
  75.     lea        @exitST,A1
  76.     move.l    A0,(A1)                ; save the address of trap into exitST
  77.  
  78.     lea        @sysenvrec,A0        ; check the current system version
  79.     move.w    #1,D0
  80.     _SysEnvirons
  81.  
  82.     lea        @hascolor,A1        ; do we have color QD ?
  83.     move.b    9(A0),(A1)
  84.  
  85.     move.w    4(A0),D0            ; system version is in D0
  86.     cmp.w    #0x700,D0            ; system seven ?
  87.     bmi.s    @system6            ; no
  88.     move.w    #65,D2                ; subtract width of two menus at top right corner
  89.     bra.s    @goon
  90. @system6
  91.     move.w    #20,D2                ; subtract width of on cue at top right corner
  92. @goon
  93.  
  94.     subq    #4,SP                ; space for handle
  95.  
  96.     move.l    #'DXCL',-(SP)        ; GetResource('DXCL',1)
  97.     move.w    #0x1,-(SP)
  98.     _GetResource
  99.  
  100.     move.l    (SP)+,A2            ; put handle to resource into A2
  101.     move.l    A2,D3                ; save a copy of the resource handle into D3
  102.     movea.l    (A2),A2                ; dereference handle.
  103.  
  104.     lea        @last,A0            ; calculate size of patch
  105.     lea        @ThePatch,A1
  106.     suba.l    A1,A0
  107.     move.l    A0,D0
  108.     move.l    D0,D4                ; save D4 for BlockMove
  109.     _NewPtr    SYS                    ; allocate ptr in system heap /D0 used
  110.     bne.s    @noPatch            ; if error, exit
  111.  
  112.     move.l    A0,-(SP)            ; save ptr onto heap
  113.     move.w    #SystemTask,D0
  114.     _SetTrapAddress                ; set the address of the new patch, A0/D0 used
  115.  
  116.     lea        @ThePatch,A0
  117.     move.l    (SP),A1
  118.     move.l    D4,D0
  119.     _BlockMove                    ; move the patch code into the system heap, A0/A1/D0 used
  120.  
  121.     lea        @datarec,A0            ; calculate size of patch
  122.     lea        @ThePatch,A1
  123.     suba.l    A1,A0                ; offset of datarec from beginning of patch
  124.     adda.l    (SP)+,A0            ; add address of patch in sysheap. A0 contains the dest address
  125.     move.l    A0,A1                ; put A0 into A1 for BlockMove (A1 is dest address)
  126.  
  127.     move.l    A0,pointer(A2)        ; store ptr to data into the resource
  128.     move.w    D2,offset(A2)        ; store offset into the resource
  129.  
  130.     move.l    A2,A0
  131.     move.l    #34,D0                ; rec is 34 bytes long
  132.     _BlockMove
  133.  
  134.     move.l    D3,-(SP)            ; save back the resource, because we've changed the pointer value
  135.     _ChangedResource
  136.     move.l    D3,-(SP)
  137.     _WriteResource
  138.  
  139. @noPatch                        ; no patch, exit
  140.  
  141.     move.l    (SP)+,D4
  142.     
  143. ;    move.w    #128,-(SP)
  144. ;    move.w    #-1,-(SP)
  145. ;    bsr        ShowINIT
  146.     rts
  147.  
  148. @sysenvrec                        ; record filled by _SysEnvirons
  149.     DC.W    0
  150.     DC.W    0
  151.     DC.W    0
  152.     DC.W    0
  153.     DC.B    0
  154.     DC.B    0
  155.     DC.W    0
  156.     DC.W    0
  157.     DC.W    0
  158.  
  159. ;************************* the patch *********************************************/
  160.  
  161. @ThePatch
  162.  
  163.     movem.l    D0-D3/A0-A2,-(SP)    ; save some registers
  164.  
  165.     tst.b    WWExist                ; Window Manager initialized ?
  166.     bne        @goodbye            ; No : exit
  167.     tst.w    MBarHeight            ; is there a menu bar ?
  168.     beq        @goodbye            ; No : exit
  169.  
  170.     moveq    #0,D3                ; D3:Boolean : draw the date ?
  171.  
  172.     move.w    @leftrect,D0
  173.  
  174.     move.l    MenuList,A1            ; we can draw the time if there is no menu on the clock's rect
  175.     move.l    (A1),A1
  176.     move.w    2(A1),D1            ; 2(A1) holds the right coord of last menu
  177.     cmp.w    D0,D1                ; D0 holds the left coord of the clock's rect
  178.     bgt        @goodbye            ; sorry, can't do that
  179.  
  180.     move.l    #EventQueue,A0        ; test if there is a mouse down in the clock's rect
  181.     move.l    OFFSET(QHdr,qHead)(A0),A0
  182.     beq.s    @drawtime                ; empty event queue ? yes, draw the time
  183.  
  184.     cmp.w    #mouseDown,OFFSET(EvQEl,evtQWhat)(A0)    ; is it a mouse down ?
  185.     bne.s    @drawtime                                ; no, continue
  186.  
  187.     subq    #2,sp                                    ; space for result
  188.     move.l    OFFSET(EvQEl,evtQWhere)(A0),-(SP)        ; push the point
  189.     pea        @therect                                ; push the rect
  190.     _PtInRect
  191.     move.b    (SP)+,D3
  192.     bne.s    @drawdate                        ; Pt is in rect, so draw the date
  193.  
  194. @drawtime
  195.  
  196.     move.l    @lasttime,D1        ; should we draw the time ?
  197.     cmp.l    Ticks,D1
  198.     bgt        @goodbye            ; no, exit
  199.  
  200. @drawdate
  201.     pea        @saveport            ; saves the current port
  202.     _GetPort
  203.     
  204. ;    _Debugger
  205. ;    move.l    @saveport,A0
  206. ;    move.w    OFFSET(GrafPort,portRect)(A0),D0
  207. ;    move.w    MBarHeight,D1
  208. ;    cmp.w    D0,D1
  209. ;    ble        @goodbye
  210.  
  211.     move.w    @hascolor,D0        ; are we in color ?
  212.     bne        @initcolor
  213.  
  214.     move.l    WMgrPort,-(SP)        ; sets the port to the window manager port
  215.     _SetPort
  216.  
  217.     move.l    WMgrPort,A0            ; A0 holds a pointer to the window manager port
  218.  
  219.     move.w    OFFSET(GrafPort,txFont)(A0),-(SP)        ; save some params on the stack
  220.     move.w    OFFSET(GrafPort,txSize)(A0),-(SP)
  221.     move.w    OFFSET(GrafPort,txMode)(A0),-(SP)
  222.     bra.s    @endinit
  223.  
  224. @initcolor
  225.  
  226.     move.l    WMgrCPort,-(SP)        ; sets the port to the color window manager port
  227.     _SetPort
  228.  
  229.     move.l    WMgrCPort,A0        ; A0 holds a pointer to the color window manager port
  230.  
  231.     move.w    OFFSET(CGrafPort,txFont)(A0),-(SP)        ; save some params on the stack
  232.     move.w    OFFSET(CGrafPort,txSize)(A0),-(SP)
  233.     move.w    OFFSET(CGrafPort,txMode)(A0),-(SP)
  234.     lea        @savedFGcolor,A1
  235.  
  236.     move.l    OFFSET(CGrafPort,rgbFgColor)(A0),(A1)+    ; save the foreground color
  237.     move.l    OFFSET(CGrafPort,rgbFgColor+4)(A0),(A1)+
  238.     move.l    OFFSET(CGrafPort,rgbFgColor+8)(A0),(A1)    ; save the background color
  239.  
  240. /* the lines above were originally :
  241.     
  242.     move.l    OFFSET(CGrafPort,rgbFgColor)(A0),(A1)+    ; save the foreground color
  243.     move.w    OFFSET(CGrafPort,rgbFgColor+4)(A0),(A1)+
  244.     move.l    OFFSET(CGrafPort,rgbBkColor)(A0),(A1)+    ; save the background color
  245.     move.w    OFFSET(CGrafPort,rgbBkColor+4)(A0),(A1)
  246.  
  247.     so we've saved 8 bytes !
  248. */    
  249.     move.l    #RGBWhite,-(SP)            ; push default colors on the stack
  250.     move.l    #RGBBlack,-(SP)
  251.     
  252.     move.w    ChunkyDepth,D0            ; if we are in one bit mode,
  253.     subq.w    #1,D0
  254.     beq.s    @setcolor                ; don't use MCInfo
  255.  
  256.     move.l    MenuCInfo,A0
  257.     move.l    (A0),A0
  258.     cmp.w    #-99,(A0)                ; item id is -99 if there is no MC entry
  259.     beq.s    @noMCentry
  260.  
  261.     lea        mctRGB1(A0),A1            ; push foreground color
  262.     move.l    A1,(SP)
  263.  
  264.     lea        mctRGB4(A0),A1            ; push background color
  265.     move.l    A1,4(SP)
  266.  
  267. @noMCentry
  268.     move.w    @useclockcolor,D0        ; if we should use the clock's color
  269.     beq.s    @setcolor
  270.     lea        @thecolor,A1
  271.     move.l    A1,(SP)
  272. @setcolor
  273.     _RGBForeColor                    ; set the foreground & background color
  274.     _RGBBackColor
  275.  
  276. @endinit
  277.  
  278.     pea        @therect
  279.     _ClipRect                    ; set the clipping region. (we dont save the old one)
  280.  
  281.     move.w    @leftrect,-(SP)
  282.     move.w    @vpos,-(SP)            ; v coord
  283.     _MoveTo
  284.  
  285.     move.w    @font,-(SP)            ; set the font, size & mode
  286.     _TextFont
  287.     move.w    @size,-(SP)
  288.     _TextSize
  289.     move.w    #srcCopy,-(SP)
  290.     _TextMode
  291.  
  292.     lea        @DateTimeRec,A0        ; converts the date & time to num
  293.     move.l    Time,D0
  294.     _Secs2Date
  295.  
  296.     lea        @loop,A1            ; let's do some self modifying code !
  297.     move.w    #':',2(A1)            ; use ':' to draw the time
  298.     lea        @a,A1
  299.     move.w    #0x3018,(A1)        ; move.w    (A0)+,D0
  300.     tst.b    D3
  301.     beq.s    @addoffset
  302.  
  303.     sub.w    #1900,(A0)            ; wont work in the 21th century !
  304.     lea        @loop,A1
  305.     move.w    #'.',2(A1)            ; use '.' to draw the date
  306.     lea        @a,A1
  307.     move.w    #0x3020,(A1)        ; move.w    -(A0),D0
  308. @addoffset
  309.     addq.w    #6,A0                ; A0+6 points to the time
  310.     lea        @thestring,A1
  311.     move.b    #9,(A1)+            ; the string is 9 bytes long
  312.  
  313.     moveq    #02,D2                ; loop 3 times
  314.     bra.s    @loop1                ; skip ':' at start
  315. @loop
  316.      move.b    #':',(A1)+            ; this line can be modified by the code above
  317. @loop1
  318.      moveq    #0,D0
  319. @a    move.w    (A0)+,D0
  320.     divu.w    #10,D0
  321.     or.b    #0x30,D0            ; ASCII('0') is 0x30
  322.     move.b    D0,(A1)+
  323.     swap    D0
  324.     or.b    #0x30,D0            ; ASCII('0') is 0x30
  325.     move.b    D0,(A1)+
  326.     dbf        D2,@loop
  327.  
  328.     pea        @thestring
  329.     _DrawString                    ; draw the string
  330.  
  331.     _TextMode                    ; restore things
  332.     _TextSize
  333.     _TextFont
  334.     move.w    @hascolor,D0
  335.     beq.s    @endrestore
  336.  
  337. @restorecolor
  338.  
  339.     pea        @savedFGcolor        ; restore the foreground & background colors
  340.     _RGBForeColor
  341.     pea        @savedBKcolor
  342.     _RGBBackColor
  343.  
  344. @endrestore
  345.  
  346.     move.l    @saveport,-(SP)        ; restore current port
  347.     _SetPort
  348.  
  349.     lea        @lasttime,A0        ; set the next time to 30 ticks ahead
  350.     move.l    Ticks,(A0)
  351.     add.l    #30,(A0)
  352.     tst.b    D3
  353.     beq.s    @goodbye
  354.     add.l    #180,(A0)
  355.  
  356. @goodbye
  357.  
  358.     movem.l    (SP)+,D0-D3/A0-A2        ; restore registers
  359.     dc        JMP
  360. @exitST                                ; here comes the address of the original patch
  361.     nop
  362.     nop
  363.     
  364. @savedFGcolor    DC.W    0        ; saved foreground color for CGrafPort
  365.                 DC.W    0
  366.                 DC.W    0
  367. @savedBKcolor    DC.W    0        ; saved background color for CGrafPort
  368.                 DC.W    0
  369.                 DC.W    0
  370.  
  371.  
  372. @thestring    DC.L    0        ; thestring holds the time. 10 bytes long.
  373.             DC.L    0
  374.             DC.W    0x0020    ; last char is always a space.
  375.  
  376. @saveport    DC.L    0        ; saved port
  377.  
  378. @lasttime    DC.L    0        ; last time we've drawn the clock
  379.  
  380. @DateTimeRec                ; holds the converted date & time
  381.  
  382. @year        DC.W    0
  383. @month        DC.W    0
  384. @day        DC.W    0
  385. @hour        DC.W    0
  386. @minute        DC.W    0
  387. @second        DC.W    0
  388. @dayOfW        DC.W    0
  389.  
  390. @hascolor    DC.W    0        ; has color QD ?
  391.  
  392. @datarec                    ; we store the handle 'DXCL' here
  393.  
  394. @tnam        DC.L    0        ; identifier of the resource
  395. @pointer    DC.L    0        ; loc of the ptr
  396. @font        DC.W    0        ; font used
  397. @size        DC.W    0        ; size of the font
  398. @offset        DC.W    0        ; offset from left
  399. @stringwid    DC.W    0        ; width of time
  400. @vpos        DC.W    0        ; v coordinate
  401. @thecolor
  402. @red        DC.W    0        ; red color
  403. @green        DC.W    0        ; green color
  404. @blue        DC.W    0        ; blue color
  405.  
  406. @therect                    ; rect used for clipping
  407. @toprect    DC.W    0
  408. @leftrect    DC.W    0
  409. @bottomrect    DC.W    0
  410. @rightrect    DC.W    0
  411. @useclockcolor    DC.W    0    ; use clock's color instead of menu bar's
  412.  
  413. @last
  414.     }
  415. }
  416.